#!user/bin/env python3
# -- coding: utf-8 --
# @Createtime：2022/5/18
# @Updatetime：22:44
# @Author：Test008
# @File：SortStuInfo.py
# @Description：根据学生成绩进行排序，统计学生的总分
from SearchStuInfo import show_student
from config import filename
import os


def sort():
    global asc_or_desc_bool
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as rfile:
            student_old = rfile.readlines()
        student_dict = []
        for item in student_old:
            d = dict(eval(item))
            student_dict.append(d)
        #     print(type(d), d)
        # print(student_dict)

    else:
        return
    asc_or_desc = input('请选择(0.升序 1.降序)：')
    if asc_or_desc == '0':
        asc_or_desc_bool = False
    elif asc_or_desc == '1':
        asc_or_desc_bool = True
    else:
        print('您输入有误，请重新输入。')
        sort()
    mode = input('请选择(1.按照英语成绩排序 2.按照Python成绩排序 3.按照Java成绩排序 0.按照总成绩排序)：')
    if mode == '1':
        student_dict.sort(key=lambda x: int(x['english']), reverse=asc_or_desc_bool)
    elif mode == '2':
        student_dict.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool)
    elif mode == '3':
        student_dict.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool)
    elif mode == '0':
        student_dict.sort(key=lambda x: int(x['english'])+int(x['python'])+int(x['java']), reverse=asc_or_desc_bool)
    else:
        print('您的输入有误，请重新输入：')
        sort()
    show_student(student_dict)


if __name__ == '__main__':
    sort()
